﻿using System.Collections;
using System.Data;

namespace MaLu.BusiLogic
{
    public class BLJson
    {

        /// <summary>
        /// 把DataTable转成flexiGrid需要的Json串
        /// </summary>
        /// <param name="dt">需要转换的DataTable数据</param>
        /// <param name="Columns">需要获取的列名称集合</param>
        /// <param name="PKColumns">主键列名集合</param>
        /// <param name="PKColumnSplit">PKColumn分隔符</param>
        /// <param name="currentPageIndex">当前页码，从1开始的</param>
        /// <param name="recordCount">总计行数</param>
        /// <returns></returns>
        public static string ToFlexiGrid(DataTable dt, ArrayList Columns, ArrayList PKColumns, string PKColumnSplit, int currentPageIndex, int recordCount)
        {
            string result = "{{\"page\":{0},\"total\": {1},\"rows\":[{2}]}}";
            string rows = "";
            string rowstemp = "";
            foreach (DataRow dr in dt.Rows)
            {
                string id = "";
                foreach (string pk in PKColumns)
                {
                    id += dr[pk] + PKColumnSplit;
                }
                if (id.Length > 0 && PKColumnSplit.Length > 0)
                    id = id.Substring(0, id.Length - PKColumnSplit.Length);

                string columns = "";
                string temp = "";
                foreach (string c in Columns)
                {
                    if (c != string.Empty)
                    {
                        columns += temp + "\"" + dr[c] + "\"";
                        temp = ",";
                    }
                    else
                    {
                        columns += temp + "\"\"";
                        temp = ",";
                    }
                }

                string row = string.Format("{{\"id\":\"{0}\",\"cell\":[{1}]}}", id, columns);
                rows += rowstemp + row;
                rowstemp = ",";

            }
            return string.Format(result, currentPageIndex, recordCount, rows);

        }

        /// <summary>
        /// DataTable to Json
        /// </summary>
        /// <param name="dt">需要转换的DataTable数据</param>
        /// <param name="Columns">需要获取的列名称集合</param>
        /// <returns></returns>
        public static string ToJson(DataTable dt, ArrayList Columns)
        {
            return ToJson(dt, Columns, int.MinValue);
        }

        /// <summary>
        /// DataTable to Json
        /// </summary>
        /// <param name="dt">需要转换的DataTable数据</param>
        /// <param name="Columns">需要获取的列名称集合</param>
        /// <param name="MaxCount">需要转换的行数，int.MinValue时，转换所有行</param>
        /// <returns></returns>
        public static string ToJson(DataTable dt, ArrayList Columns, int MaxCount)
        {
            if (MaxCount == int.MinValue)
                MaxCount = dt.Rows.Count;
            if (MaxCount > dt.Rows.Count)
                MaxCount = dt.Rows.Count;
            string json = "";
            string temp = "";
            for(int i = 0; i < MaxCount; i ++)
            {
                DataRow dr = dt.Rows[i];
                string row = "";
                string rowtemp = "";
                foreach (string c in Columns)
                {
                    string col = string.Format("\"{0}\":\"{1}\"", c, dr[c]);
                    row += rowtemp + col;
                    rowtemp = ",";
                }
                json += temp + "{" + row + "}";
                temp = ",";
            }

            return "{\"rows\":[" + json + "]}";
        }

       

        /// <summary>
        /// DataTable转ZTree需要的数据，单层，没有做递归
        /// </summary>
        /// <param name="dt">需要转换的DataTable数据</param>
        /// <param name="NameColumn">显示列名称</param>
        /// <param name="IDColumn">ID列名称</param>
        /// <param name="IsLeafColumn">是否是叶子节点的列名</param>
        /// <param name="IcoColumn">图片列明</param>
        /// <param name="IcoFixPath">图片地址附加path</param>
        /// <returns></returns>
        public static string ToZTreeJson(DataTable dt, string NameColumn, string IDColumn, string IsLeafColumn, string IcoColumn, string IcoFixPath)
        {
            string temp = "";
            string json = "";
            foreach (DataRow dr in dt.Rows)
            {
                if (IcoColumn != string.Empty)
                    json += temp + string.Format("{{name:\"{0}\",id:\"{1}\" , isParent:{2},icon:\"{3}\"}}", dr[NameColumn], dr[IDColumn], bool.Parse(dr[IsLeafColumn].ToString()) ? "false" : "true",IcoFixPath + dr[IcoColumn]);
                else
                    json += temp + string.Format("{{name:\"{0}\",id:\"{1}\" , isParent:{2}}}", dr[NameColumn], dr[IDColumn], bool.Parse(dr[IsLeafColumn].ToString()) ? "false" : "true");
                temp = ",";
            }
            return "[" + json + "]";
        }
    }
}
